<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>镜像列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/x-admin/css/font.css">
    <link rel="stylesheet" href="/x-admin/css/xadmin.css">

    <link rel="stylesheet" href="/x-admin/lib/layui/css/modules/layer/default/layer.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript" src="/x-admin/lib/layui/layui.js" charset="utf-8"></script>

    <script type="text/javascript" src="/x-admin/lib/layui/lay/modules/layer.js" charset="utf-8"></script>

    <script type="text/javascript" src="/x-admin/js/xadmin.js"></script>
    <script type="text/javascript" src="/x-admin/js/vue.js"></script>
    <script type="text/javascript" src="/x-admin/js/axios.js"></script>
    <script type="text/javascript" src="/x-admin/js/common.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.2.2/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui@2.2.2/lib/index.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="/page/welcome">起始页</a>
            <a href="/image/page/list"><cite>镜像列表</cite></a>
        </span>

    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:window.location.reload();" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i>
    </a>
</div>
<div class="x-body">
    <el-row>
        <el-col :span="2">
            <el-tree :data="data" :props="defaultProps" node-key="id" highlight-current="true"
                     @node-click="getImage"></el-tree>
        </el-col>
        <el-col :span="1">
            &nbsp;
        </el-col>
        <el-col :span="21">
            <xblock>
                <button class="layui-btn" onclick="window.location.href='/image/page/add'"><i class="layui-icon"></i>添加
                </button>
                <span class="x-right" style="line-height:40px">共有数据：{{dataModel.total}} 条</span>
            </xblock>
            <table class="layui-table">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>机器名字</th>
                    <th>镜像名字</th>
                    <th>镜像地址</th>
                    <th>镜像状态</th>

                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(data, index) in dataModel.list">
                    <td>{{index+1}}</td>
                    <td>{{data.machineName}}</td>
                    <td>{{data.name}}</td>
                    <td>{{data.path}}</td>
                    <td>{{data.state}}</td>

                    <td>
                        <el-button @click="toUpdatePage(data.id)">修改</el-button>
                        <el-button @click="addContainer(data.id,data.machineId,data.name)">创建容器</el-button>
                        <el-button @click="deleteData(data.id,index)">删除</el-button>
                    </td>
                </tr>
                </tbody>
            </table>
            <div class="page">
                <div>
                    <a class="prev" href="#" v-if="dataModel.hasPreviousPage"
                       @click="getDatas(dataModel.pageNum-1)">&lt;&lt;</a>
                    <a class="num" href="#" v-if="dataModel.hasPreviousPage"
                       @click="getDatas(dataModel.pageNum-1)">{{dataModel.pageNum-1}}</a>
                    <span class="current">{{dataModel.pageNum}}</span>
                    <a class="num" href="#" v-if="dataModel.hasNextPage" @click="getDatas(dataModel.pageNum+1)">{{dataModel.pageNum+1}}</a>
                    <a class="num" href="#" v-if="dataModel.pageNum+2<dataModel.pages"
                       @click="getDatas(dataModel.pages)">{{dataModel.pages}}</a>
                    <a class="next" href="#" v-if="dataModel.hasNextPage" @click="getDatas(dataModel.pageNum+1)">&gt;&gt;</a>
                </div>
            </div>
        </el-col>
    </el-row>


</div>
<script type="text/javascript">
    //axios http库
    var instance = axios.create({
        baseURL: "",
        timeout: 5000
    });
    axios.interceptors.request.use(function (config) {
        return config;
    }, function () {
        console.error("request阶段处理异常", error)
        return Promise.reject(error);
    })


    //axios response interceptor
    instance.interceptors.response.use(function (res) {
        console.log(res)
        if (res && res.data && res.data.code === 200 || res.data.code === 0)
            return res;
        else {
            layer.msg("请求异常" + res.data.msg);
            return Promise.reject(res.data);
        }
        return res;
    }, function (error) {
        layer.msg("网络处理异常")
        console.error("网络处理异常", error)
        return Promise.reject(error);
    })

    //set vue use axios
    Vue.prototype.$http = instance;


    $(function () {

        var vue = new Vue({
            el: '.x-body',
            data: {
                dataModel: [],
                searchModel: {},
                defaultProps: {
                    label: 'machineName'
                },
                data: []
            },
            methods: {
                getDatas: function (page) {
                    var thiz = this;
                    this.$http.post("/image/search" + "?" + getPageQueryStr(page, 10), clearEmptyValue(this.searchModel)).then(function (data) {
                        var data = data.data.data;
                        thiz.dataModel = data;
                    })
                },
                deleteData: function (id, index) {
                    if (!id)
                        alert("删除失败，Id数据缺失！")
                    var thiz = this;
                    var confirmResult = confirm("确认要删除数据！")
                    if (confirmResult) {
                        this.$http.delete("/image/" + id).then(function (data) {
                            alert("删除成功！");
                            thiz.dataModel.list.splice(index, 1);
                        })
                    }
                },
                toUpdatePage: function (id) {
                    window.location.href = "/image/page/update?id=" + id;
                },
                toAddPage: function () {
                    window.location.href = "/image/page/update?id=add"
                },
                handleUploadSuccess: function () {
                    this.dataModel.img = (file && file[0]) ? (fileResourcesDomain + file[0].filepath) : "";
                },
                addContainer: function (imgId, machineId, imgName) {
                    window.location.href = "/container/page/add?imgId=" + imgId + "&machineId=" + machineId + "&imgName=" + imgName;
                },
                getMachine: function () {
                    var thiz = this;
                    this.$http.get("/machine?size=1000").then(function (data) {
                        thiz.data = data.data.data.list;
                    })
                },
                getImage: function (data) {
                    this.searchModel.machineId = data.id;
                    this.getDatas()
                }
            },
            updated: function () {
                $('.tablelist tbody tr:odd').addClass('odd');
            },
            mounted: function () {
                var machineId = getParam("machineId");
                if (machineId) {
                    this.searchModel.machineId = machineId;
                    this.getDatas();
                }
                this.getMachine();
            }
        });
    });
</script>

</body>

</html>